home *** CD-ROM | disk | FTP | other *** search
/ Shocking The Web CD-ROM / SHOCK_CD.ISO / pc / tutorial / devglry / zoetek / source / shckclck.dir / 00093_Date Routines.ls < prev    next >
Encoding:
Text File  |  1996-04-11  |  3.0 KB  |  117 lines

  1. global datemode, dayOffset, day1, sMode, smallnil
  2.  
  3. on dateInit
  4.   set datemode to [#month: 1, #day: 2, #year: 3]
  5.   set dayOffset to 0
  6. end
  7.  
  8. on date
  9.   legalMode()
  10.   set x to the result
  11.   set day to the day of x + dayOffset
  12.   set month to the month of x
  13.   set year to the year of x
  14.   lastday(month, year)
  15.   if day > the result then
  16.     set day to 1
  17.     set year to (100 + year + (month = 12)) mod 100
  18.     set month to (month mod 12) + 1
  19.   end if
  20.   if day < 1 then
  21.     set year to (100 + year - (month = 1)) mod 100
  22.     set month to ((month + 10) mod 12) + 1
  23.     lastday(month, year)
  24.     set day to the result
  25.   end if
  26.   dateDisplay(month, day, year)
  27. end
  28.  
  29. on dateDisplay month, day, year
  30.   DayOfWeek(month, day, year)
  31.   set dcast to the result + day1 - 1
  32.   set the castNum of sprite sMode to dcast
  33.   set x to the month of datemode < the day of datemode
  34.   donum(14 - (3 * x), smallnil, 1, month)
  35.   donum(11 + (3 * x), smallnil, 1, day)
  36.   set the castNum of sprite (20 - x) to the number of cast "month"
  37.   set the castNum of sprite (19 + x) to the number of cast "unmonth"
  38. end
  39.  
  40. on legalMode
  41.   extractDate()
  42.   set date to the result
  43.   set legal to 0
  44.   repeat while not legal
  45.     set day to getAt(date, the day of datemode)
  46.     set month to getAt(date, the month of datemode)
  47.     set year to getAt(date, the year of datemode)
  48.     if (month <= 12) and (month >= 1) then
  49.       lastday(month, year)
  50.       if (day <= the result) and (day >= 1) then
  51.         set legal to 1
  52.       end if
  53.     end if
  54.     if not legal then
  55.       nextmode()
  56.     end if
  57.   end repeat
  58.   return [#day: day, #month: month, #year: year]
  59. end
  60.  
  61. on extractDate
  62.   set date to the date
  63.   set del1 to 0
  64.   set del2 to 0
  65.   set len to the length of date
  66.   repeat with x = 1 to len
  67.     if not integerp(integer(char x of date)) or (char x of date = "-") then
  68.       if not del1 then
  69.         set del1 to x
  70.         next repeat
  71.       end if
  72.       set del2 to x
  73.     end if
  74.   end repeat
  75.   set d1 to integer(chars(date, 1, del1 - 1))
  76.   set d2 to integer(chars(date, del1 + 1, del2 - 1))
  77.   set d3 to integer(chars(date, del2 + 1, len))
  78.   return [d1, d2, d3]
  79. end
  80.  
  81. on DayOfWeek month, day, year
  82.   if year > 100 then
  83.     set cent to year / 100
  84.     set year to year mod 100
  85.   else
  86.     set cent to 19 + (year < 95)
  87.     set year to (100 + year) mod 100
  88.   end if
  89.   set val to (year / 4) + day + getAt([1, 4, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6], month)
  90.   if not (year mod 4) and (month <= 2) then
  91.     set val to val - 1
  92.   end if
  93.   if cent = 20 then
  94.     set val to val + 6
  95.   end if
  96.   set val to val + year
  97.   set val to val mod 7
  98.   return 1 + ((7 + val - 1) mod 7)
  99. end
  100.  
  101. on lastday month, year
  102.   set feb to 28 + not (year mod 4)
  103.   return getAt([31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], month)
  104. end
  105.  
  106. on nextmode
  107.   set m to the month of datemode
  108.   set d to the day of datemode
  109.   set y to the year of datemode
  110.   set switch to (y = (d + 2)) - (y = (m - 2))
  111.   set the month of datemode to d + switch
  112.   set the day of datemode to m + switch
  113.   if switch then
  114.     set the year of datemode to y - (2 * switch)
  115.   end if
  116. end
  117.